package com.semanticweb.project.search.api;

import java.io.File;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;

import com.semanticweb.project.search.searchers.QuerySearcher;

public class SearchAPIImpl implements SearchAPI {

    private static final ResourceBundle SEARCH_BUNDLE = ResourceBundle.getBundle("search");
    public static final String INDEX_DIR_PROPERTY = "indexDir";
    public static final String DATA_DIR_PROPERTY = "dataDir";

    @Override
    public boolean index(final File indexDir, final AbstractDocument doc) {
        final Indexer indexer = new Indexer();
        try {
            final Directory dir = new SimpleFSDirectory(indexDir);
            indexer.index(dir, doc);
        } catch (final Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean index(final AbstractDocument doc) {
        final File indexDir = new File(SEARCH_BUNDLE.getString(INDEX_DIR_PROPERTY));
        return index(indexDir, doc);
    }

    @Override
    public List<String> search(File indexDir, final String query, final String field, final int docCount) {
        if (indexDir == null) {
            indexDir = new File(SEARCH_BUNDLE.getString(INDEX_DIR_PROPERTY));
        }
        return QuerySearcher.search(indexDir, query, field, docCount);
    }

    @Override
    public List<String> search(final String query, final String field) {
        return search(null, query, field, 10);
    }
}
